from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings


def dumps(json, expires):
    serializer = Serializer(settings.SECRET_KEY, expires)
    json_byte = serializer.dumps(json)
    return json_byte.decode()


def loads(json_str, expires):
    serializer = Serializer(settings.SECRET_KEY, expires)
    try:
        json = serializer.loads(json_str)
    except:
        return None
    else:
        return json
